﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace SemesterProjekt
{
    /*
     * delegate -> how i am sending stuff (object sender, XEventArgs ..)
     * event -> that i am going to send stuff
     * On..() -> securing that soneones listening 
     * 
     * */
    class Sensor
    {
        public delegate void ValueChangedEventHandler(Object sender, ValueChangedEventArgs args);
        public event ValueChangedEventHandler ValueChanged;       
        Timer timer;


        public Sensor()
        {
            timer = new Timer(1000);
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Enabled = true;
            timer.Start();


        }

        private void timer_Elapsed(Object sender, ElapsedEventArgs args) {
            Measure();
        }

     

        protected void OnValueChanged(double newValue)
        {
            if (ValueChanged != null)
            {
                ValueChangedEventArgs args = new ValueChangedEventArgs();
                args.NewValue = newValue;
                ValueChanged(this, args);
            }
        }

        public void Measure()
        {
            Random r = new Random();
            double value = r.NextDouble() * 10;
            OnValueChanged(value);
        }

    }
}
